/*
 *
 *   This file is part of jSevenBar. Copyright 2011 Alex Volodko
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */
package de.xhelp.jsevenbar.demos;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.LayoutStyle;
import javax.swing.WindowConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import de.xhelp.jsevenbar.core.ProgressBarState;

/**
 * @author Alex Volodko
 * 
 */
public class DemoProgressBar extends AbstractDemo {

	private JComboBox type;
	private JSlider value;

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.xhelp.jsevenbar.demos.AbstractDemo#getDemoName()
	 */
	@Override
	protected String getDemoName() {
		return "Progress bar demo";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.xhelp.jsevenbar.demos.AbstractDemo#initiateListeners()
	 */
	@Override
	protected void initiateListeners() {
		type.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JComboBox source = (JComboBox) e.getSource();
				ProgressBarState state = (ProgressBarState) source
						.getSelectedItem();
				ownSevenBar.getProgressBar().setState(state);
			}
		});

		value.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent e) {
				JSlider source = (JSlider) e.getSource();
				ownSevenBar.getProgressBar().setValue(source.getValue());
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.xhelp.jsevenbar.demos.AbstractDemo#initiateDemo()
	 */
	@Override
	protected void initiateDemo() {
		GroupLayout thisLayout = new GroupLayout((JComponent) getContentPane());
		getContentPane().setLayout(thisLayout);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		JLabel typeLb = new JLabel();
		typeLb.setText("Progress bar type:");
		ComboBoxModel typeCBModel = new DefaultComboBoxModel(
				new ProgressBarState[] { ProgressBarState.NOPROGRESS,
						ProgressBarState.INDETERMINATE,
						ProgressBarState.NORMAL, ProgressBarState.PAUSED,
						ProgressBarState.ERROR });
		type = new JComboBox();

		type.setModel(typeCBModel);
		value = new JSlider();
		value.setValue(0);

		thisLayout.setVerticalGroup(thisLayout
				.createSequentialGroup()
				.addContainerGap()
				.addGroup(
						thisLayout
								.createParallelGroup(
										GroupLayout.Alignment.BASELINE)
								.addComponent(type,
										GroupLayout.Alignment.BASELINE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addComponent(typeLb,
										GroupLayout.Alignment.BASELINE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(value, GroupLayout.PREFERRED_SIZE,
						GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
		thisLayout
				.setHorizontalGroup(thisLayout
						.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								thisLayout
										.createParallelGroup()
										.addGroup(
												GroupLayout.Alignment.LEADING,
												thisLayout
														.createSequentialGroup()
														.addComponent(
																typeLb,
																GroupLayout.PREFERRED_SIZE,
																109,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(type, 0,
																305,
																Short.MAX_VALUE))
										.addComponent(value,
												GroupLayout.Alignment.LEADING,
												0, 414, Short.MAX_VALUE))
						.addContainerGap());
		pack();
		this.setSize(454, 113);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.xhelp.jsevenbar.demos.AbstractDemo#makeModular()
	 */
	@Override
	public void makeModular() {
		// do nothing
	}
	public static void main(String[] args) {
		new DemoProgressBar();
	}

}
